package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 创建护理计划VO对象并复制基础属性
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan=getById( id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        // 查询关联的护理项目计划列表
        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));

        // 转换护理项目计划为VO对象
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());

        // 设置护理项目计划列表并返回结果
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }


    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划数据传输对象，包含护理计划的基本信息和关联的项目计划列表
     * @return 新增结果，成功返回1，失败返回0
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 将DTO对象转换为实体对象并设置创建时间
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        // 批量插入关联的护理项目计划
        int count = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());

        // 删除缓存以保证数据一致性
        redisTemplate.delete(CACHE_KEY);
        return count == 0 ? 0 : 1;
    }


    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划数据传输对象，包含要更新的护理计划信息
     * @return 更新结果，成功返回1，失败返回0
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 更新护理项目计划列表
        List<NursingProjectPlan> nursingPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(nursingPlans)){
            // 删除原有的护理项目计划
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            // 设置新的护理项目计划关联ID
            nursingPlans.forEach(nursingProjectPlan ->
                    nursingProjectPlan.setPlanId(Long.valueOf(nursingPlanDto.getId()+ ""))
            );
            // 批量保存新的护理项目计划
            iNursingProjectPlanService.saveBatch(nursingPlans);
        }
        // 更新护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        boolean update = updateById(nursingPlan);
        //删除缓存
        redisTemplate.delete(CACHE_KEY);
        return update ? 1 : 0;
    }


    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean remove = removeById(id);
        //删除缓存
        redisTemplate.delete(CACHE_KEY);
        return remove ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean flag = removeById(id);
        //删除缓存
        redisTemplate.delete(CACHE_KEY);
        return flag ? 1 : 0;
    }

    /**
     * 查询所有护理计划信息
     * @return 护理计划列表
     */
    @Override
    public List<NursingPlan> listAll() {
        // 从Redis缓存中获取护理计划列表
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (list != null&&list.size()>0){
            return list;
        }
        // 缓存未命中时，从数据库查询状态为1的护理计划并更新缓存
        list = list(Wrappers.<NursingPlan>lambdaQuery().eq(NursingPlan::getStatus, 1));
        redisTemplate.opsForValue().set(CACHE_KEY, list);
        return list;
    }

}
